package com.swz.dcrm.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static String currentDateFormat(String format){

        SimpleDateFormat simpleDateFormat =new SimpleDateFormat(format);

        return simpleDateFormat.format(new Date());

    }

    public static String dateFormat(Date date, String format){
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat(format);

        return simpleDateFormat.format(date);
    }


    public static String dateFormat(String date, String format){
        if(date==null){
            return "";
        }
        SimpleDateFormat simpleDateFormat;
        Date date1=null;
        if(date.length()==10){
            simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
        }else {
            simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }


        try {
            date1=simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return dateFormat(date1,format);
    }

    public static Date dateParse(String date, String format)  {
        if(date==null){
            return new Date();
        }
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat(format);

        Date date1=null;
        try {
            date1= simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date1;
    }
   public static String transferLongToDate(String dateFormat, Long millSec){

        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);

        Date date= new Date(millSec*1000);

        return sdf.format(date);

    }

    public static int compareDate(String DATE1, String DATE2) {


        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.before(dt2)) {
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.after(dt2)) {
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
    public static int compareDate1(String DATE1, String DATE2) {


        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.before(dt2)) {
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.after(dt2)) {
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
    public static int compareDate2(String DATE1, String DATE2) {


        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.before(dt2)) {
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.after(dt2)) {
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
    public static String getCurrentDate(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间

    }
    //获得几天后的日期
    public static String getDateAfter(int day){
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        return df.format(now.getTime());
    }
    //获得几天后的日期
    public static String getDateAfter(Date date,int day){
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        return df.format(now.getTime());
    }
}
